package com.tj.collection.controller.chatquality.response;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fqgj.common.utils.CollectionUtils;
import com.kly.dto.UserBenefitsDto;
import com.kly.user.dto.UserBaseDto;
import com.kly.user.dto.UserMsgDto;
import com.kly.user.dto.rongCloud.RongCloudReqContent;
import com.kly.user.enums.AlbumsType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@ApiModel(value = "messageDetailVO", description = "消息信息")
@Data
@Builder
public class MessageDetailVO implements Serializable {
    @ApiModelProperty("model用户码")
    private String modelUserCode;
    @ApiModelProperty("model昵称")
    private String modelNickName;
    @ApiModelProperty("用户码")
    private String userCode;
    @ApiModelProperty("用户昵称")
    private String nickName;
    @ApiModelProperty("消息内容")
    private String content;
    @ApiModelProperty("解锁标志：1-已解锁 0-未解锁（imageType=2时该字段有值）")
    private Integer unlockFlag;
    @ApiModelProperty("消息类型：1-public 2-private, content里存放照片时该字段有值")
    private Integer imageType;
    @ApiModelProperty("消息发送时间")
    private String sendTime;
    @ApiModelProperty("消息时间显示：当天的第一条消息显示该条消息发送时间， 格式：yyyy-MM-dd,HH:mm")
    private String noticeTime;
    @ApiModelProperty("消息发送方向：1-用户发送 2-chatter发送")
    private Integer messageDirection;
    /**
     * 消息类型，接受内置消息类型（见消息类型概述）或自定义消息的消息类型值。
     *
     * 消息类型	    ObjectName
     * 文本	        RC:TxtMsg
     * 高清语音消息	RC:HQVCMsg
     * 图片消息	    RC:ImgMsg
     * GIF 图片消息	RC:GIFMsg
     * 图文消息	    RC:ImgTextMsg
     * 文件消息	    RC:FileMsg
     * 位置消息	    RC:LBSMsg
     * 小视频消息	    RC:SightMsg
     * 引用消息	    RC:ReferenceMsg
     * 合并转发消息	RC:CombineMsg
     */
    @ApiModelProperty("消息类型：RC:TxtMsg-文本消息 RC:ImgMsg-图片 RC:GIFMsg-GIF 图片消息 RC:SightMsg-视频")
    private String msgType;

    public static MessageDetailVO toVo(UserMsgDto dto, UserBaseDto userAccount, UserBaseDto modelAccount, List<UserBenefitsDto> benefits) {
        if (dto == null) {
            return null;
        }
        MessageDetailVO messageDetailVO = MessageDetailVO.builder().msgType(dto.getMsgType()).noticeTime(dto.getSendFirstMsgTimePreDay()).sendTime(DateUtil.format(dto.getGmtCreate(), "yyyy-MM-dd,HH:mm")).modelNickName(modelAccount.getNickName()).modelUserCode(modelAccount.getUserCode()).nickName(userAccount.getNickName()).userCode(userAccount.getUserCode()).content(dto.getContent()).build();

        String msgContent = dto.getContent();
        String content;

        if (JSONUtil.isTypeJSON(msgContent)) {
            RongCloudReqContent rongCloudReqContent = JSONUtil.toBean(msgContent, RongCloudReqContent.class);
            String extra = rongCloudReqContent.getExtra();
            content = rongCloudReqContent.getContent();
            if (extra != null) {
                extra = extra.replaceAll("=", ":");
                JSONObject jsonObject = JSONUtil.parseObj(extra);
                boolean isPrivate = jsonObject.getInt("albumType") != null && AlbumsType.PRIVATE_ALBUMS.getType() == jsonObject.getInt("albumType");
                if ("RC:ImgMsg".equals(dto.getMsgType()) || "RC:SightMsg".equals(dto.getMsgType())) {
                    if (isPrivate) {
                        messageDetailVO.setImageType(2);
                    } else {
                        messageDetailVO.setImageType(1);
                    }
                }

                if (isPrivate) {
                    messageDetailVO.setUnlockFlag(0);
                    if (CollectionUtils.isNotEmpty(benefits)) {
                        final boolean unlock = benefits.parallelStream().anyMatch(o -> {
                            if (o.getMsgId() != null && o.getMsgId().equals(dto.getMsgId())) {
                                return Boolean.TRUE;
                            }
                            if (o.getCreateDt() != null && o.getCreateDt().after(dto.getGmtCreate())) {
                                return content.contains(o.getImageUrl()) || jsonObject.getStr("imageCode").equals(o.getImageCode());
                            }
                            return Boolean.FALSE;
                        });
                        if (unlock) {
                            messageDetailVO.setUnlockFlag(1);
                        }
                    }
                }
            }
            if (msgContent.contains("chatterCode")) {
                // 用户接收的消息为：2
                messageDetailVO.setMessageDirection(2);
            } else {
                // 用户发送的消息messageDirection为：1
                messageDetailVO.setMessageDirection(1);
            }
        } else {
            content = msgContent;
        }

        messageDetailVO.setContent(content);
        return messageDetailVO;
    }

    public static void main(String[] args) {
//        final String format = DateUtil.format(new Date(), "");
        System.out.println(DateUtil.format(new Date(), "yyyy-MM-dd,HH:mm"));
    }
}
